/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { useMemo } from 'react';
import type { ProColumns } from '@ant-design/pro-table';
import KbDateRange, { defaultDateRange30 } from '@/components/dateRange';
import moment from 'moment';
import type { Moment } from 'moment';
import { isArray } from 'lodash';
import { Table } from 'antd';

export const momentFormat = (date: Moment, key: string = 'YYYY-MM-DD') => moment(date).format(key);

export const formatReqData = (values: any) => {
  const { current: page, pageSize: page_size, date, collect_time, sign_time, ...rest } = values;
  let date_obj = {};
  if (isArray(date) && date.length > 0) {
    date_obj = {
      start_time: momentFormat(date[0]),
      end_time: momentFormat(date[1]),
    };
  }
  let collect_time_obj = {};
  if (isArray(collect_time) && collect_time.length > 0) {
    collect_time_obj = {
      collect_start_time: momentFormat(collect_time[0]),
      collect_end_time: momentFormat(collect_time[1]),
    };
  }
  let sign_time_obj = {};
  if (isArray(sign_time) && sign_time.length > 0) {
    sign_time_obj = {
      sign_start_time: momentFormat(sign_time[0]),
      sign_end_time: momentFormat(sign_time[1]),
    };
  }
  return {
    start_time: momentFormat(defaultDateRange30[0]),
    end_time: momentFormat(defaultDateRange30[1]),
    page,
    page_size,
    ...date_obj,
    ...collect_time_obj,
    ...sign_time_obj,
    ...rest,
  };
};

export const useDpColumns = (opt: any) => {
  const { queryConfig } = opt || {};

  const columns: ProColumns<any>[] = useMemo(() => {
    if (!queryConfig) return [];
    return [
      {
        title: '运单号',
        dataIndex: 'waybill_no',
        hideInTable: true,
      },
      {
        title: '返佣流水号',
        dataIndex: 'trade_number',
        hideInTable: true,
      },
      {
        title: '订单状态',
        dataIndex: 'order_status',
        valueType: 'select',
        valueEnum: queryConfig.order_status,
        hideInTable: true,
      },
      {
        title: '下单渠道',
        dataIndex: 'channel',
        valueType: 'select',
        valueEnum: queryConfig.channel,
        hideInTable: true,
      },
      {
        title: '下单日期',
        dataIndex: 'date',
        hideInTable: true,
        initialValue: defaultDateRange30,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange />;
        },
      },
      {
        title: '揽收时间',
        dataIndex: 'collect_time',
        hideInTable: true,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange />;
        },
      },
      {
        title: '签收时间',
        dataIndex: 'sign_time',
        hideInTable: true,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange />;
        },
      },
      {
        dataIndex: 'channel',
        title: '订单渠道',
        align: 'center',
        width: 150,
        search: false,
        ellipsis: true,
        renderText: (value) => {
          return queryConfig ? queryConfig.channel?.[value] : '';
        },
      },
      {
        title: '订单号',
        dataIndex: 'order_id',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '运单号',
        dataIndex: 'waybill_no',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '第三方订单号',
        dataIndex: 'third_party_orderid',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '寄件人省市区',
        dataIndex: 'shipper_province',
        align: 'center',
        width: 200,
        search: false,
        render: (_, record) => {
          const { shipper_province, shipper_city, shipper_district } = record;
          return (
            <>
              {shipper_province}
              {shipper_city}
              {shipper_district}
            </>
          );
        },
      },
      {
        title: '收件人省市区',
        dataIndex: 'shipping_province',
        align: 'center',
        width: 200,
        search: false,
        render: (_, record) => {
          const { shipping_province, shipping_city, shipping_district } = record;
          return (
            <>
              {shipping_province}
              {shipping_city}
              {shipping_district}
            </>
          );
        },
      },
      {
        title: '订单状态',
        dataIndex: 'order_status',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
        renderText(value) {
          return queryConfig ? queryConfig.order_status?.[value] : '';
        },
      },
      {
        title: '下单时间',
        dataIndex: 'order_created_at',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '揽收时间',
        dataIndex: 'collect_date',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '签收时间',
        dataIndex: 'sign_date',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '结算重量',
        dataIndex: 'charging_weight',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '运费',
        dataIndex: 'freight_price',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '其他费用',
        dataIndex: 'other_money',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '总费用',
        dataIndex: 'pay_price',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣时间',
        dataIndex: 'record_list',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
        render: (_: any, record) => {
          const { record_list } = record;
          if (record_list && record_list.length > 0) {
            return (
              <>
                <Table
                  rowKey={'id'}
                  dataSource={record_list}
                  columns={[
                    {
                      title: '',
                      dataIndex: 'update_at',
                      align: 'center',
                      width: 200,
                      ellipsis: true,
                    },
                    {
                      title: '',
                      dataIndex: 'mobile',
                      align: 'center',
                      width: 200,
                      ellipsis: true,
                      render: (__, _record) => {
                        return (
                          <>
                            {_record.mobile}
                            {_record.account_name}
                          </>
                        );
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'profit',
                      align: 'center',
                      width: 100,
                      ellipsis: true,
                      render: (__, _record) => {
                        return <>{_record.profit ? `${_record.profit}元` : '-'}</>;
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'status',
                      align: 'center',
                      width: 100,
                      ellipsis: true,
                      render: (__, _record) => {
                        return <>{_record.order_number ? '已返佣' : '未返佣'}</>;
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'order_number',
                      align: 'center',
                      width: 240,
                      ellipsis: true,
                    },
                  ]}
                  showHeader={false}
                  pagination={false}
                  size='small'
                />
              </>
            );
          }
          return '-';
        },
        onCell: () => {
          return {
            colSpan: 5,
          };
        },
      },
      {
        title: '返佣账号',
        dataIndex: 'Id',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣金额',
        dataIndex: 'Id',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣状态',
        dataIndex: 'Id',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣流水号',
        dataIndex: 'Id',
        align: 'center',
        width: 240,
        search: false,
        ellipsis: true,
      },
    ];
  }, [queryConfig]);

  return {
    columns,
  };
};

export const useQjDpColumns = (opt: any) => {
  const { queryConfig } = opt || {};

  const columns: ProColumns<any>[] = useMemo(() => {
    if (!queryConfig) return [];
    return [
      {
        title: '运单号',
        dataIndex: 'waybill_no',
        hideInTable: true,
      },
      {
        title: '返佣流水号',
        dataIndex: 'trade_number',
        hideInTable: true,
      },
      {
        title: '订单状态',
        dataIndex: 'order_status',
        valueType: 'select',
        valueEnum: queryConfig.order_status,
        hideInTable: true,
      },
      {
        title: '下单渠道',
        dataIndex: 'channel',
        valueType: 'select',
        valueEnum: queryConfig.channel,
        hideInTable: true,
      },
      {
        title: '下单日期',
        dataIndex: 'date',
        hideInTable: true,
        initialValue: defaultDateRange30,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange disabledDate={() => false} />;
        },
      },
      {
        title: '揽收时间',
        dataIndex: 'collect_time',
        hideInTable: true,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange disabledDate={() => false} />;
        },
      },
      {
        title: '签收时间',
        dataIndex: 'sign_time',
        hideInTable: true,
        filters: true,
        renderFormItem: () => {
          return <KbDateRange disabledDate={() => false} />;
        },
      },
      {
        dataIndex: 'channel_name',
        title: '订单渠道',
        align: 'center',
        width: 150,
        search: false,
        ellipsis: true,
      },
      {
        title: '订单号',
        dataIndex: 'id',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '运单号',
        dataIndex: 'waybill_no',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '第三方订单号',
        dataIndex: 'third_order_id',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '寄件人省市区',
        dataIndex: 'shipper_province',
        align: 'center',
        width: 200,
        search: false,
        render: (_, record) => {
          const { shipper_province, shipper_city, shipper_county } = record;
          return (
            <>
              {shipper_province}
              {shipper_city}
              {shipper_county}
            </>
          );
        },
      },
      {
        title: '收件人省市区',
        dataIndex: 'shipping_province',
        align: 'center',
        width: 200,
        search: false,
        render: (_, record) => {
          const { shipping_province, shipping_city, shipping_county } = record;
          return (
            <>
              {shipping_province}
              {shipping_city}
              {shipping_county}
            </>
          );
        },
      },
      {
        title: '订单状态',
        dataIndex: 'order_status_name',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '下单时间',
        dataIndex: 'create_at',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '揽收时间',
        dataIndex: 'collect_date',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '签收时间',
        dataIndex: 'sign_date',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '结算重量',
        dataIndex: 'charging_weight',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '运费',
        dataIndex: 'freight',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '其他费用',
        dataIndex: 'other_money',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '总费用',
        dataIndex: 'pay_price',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣时间',
        dataIndex: 'record_list',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
        render: (_: any, record) => {
          const { record_list } = record;
          if (record_list && record_list.length > 0) {
            return (
              <>
                <Table
                  rowKey={'id'}
                  dataSource={record_list}
                  columns={[
                    {
                      title: '',
                      dataIndex: 'update_at',
                      align: 'center',
                      width: 200,
                      ellipsis: true,
                    },
                    {
                      title: '',
                      dataIndex: 'mobile',
                      align: 'center',
                      width: 200,
                      ellipsis: true,
                      render: (__, _record) => {
                        return (
                          <>
                            {_record.mobile}
                            {_record.account_name}
                          </>
                        );
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'profit',
                      align: 'center',
                      width: 100,
                      ellipsis: true,
                      render: (__, _record) => {
                        return <>{_record.profit ? `${_record.profit}元` : '-'}</>;
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'status',
                      align: 'center',
                      width: 100,
                      ellipsis: true,
                      render: (__, _record) => {
                        return <>{_record.order_number ? '已返佣' : '未返佣'}</>;
                      },
                    },
                    {
                      title: '',
                      dataIndex: 'order_number',
                      align: 'center',
                      width: 240,
                      ellipsis: true,
                    },
                  ]}
                  showHeader={false}
                  pagination={false}
                  size='small'
                />
              </>
            );
          }
          return '-';
        },
        onCell: () => {
          return {
            colSpan: 5,
          };
        },
      },
      {
        title: '返佣账号',
        dataIndex: 'Id',
        align: 'center',
        width: 200,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣金额',
        dataIndex: 'Id',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣状态',
        dataIndex: 'Id',
        align: 'center',
        width: 100,
        search: false,
        ellipsis: true,
      },
      {
        title: '返佣流水号',
        dataIndex: 'Id',
        align: 'center',
        width: 240,
        search: false,
        ellipsis: true,
      },
    ];
  }, [queryConfig]);

  return {
    columns,
  };
};
